﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WeddingEventManagement_API;
using WeddingEventManagementApp.Models;

namespace WeddingEventManagementApp.Controllers
{
    public class HomeController : BaseController
    {
        IDashboardManager api;

        public HomeController() 
        {
            api = new DashboardManager();
        }

        public ActionResult Index()
        {
            Dashboard_VM model = new Dashboard_VM();

            model.UpcomingEvents = api.UpcomingEvents();
            model.OutstandingInvoices = api.OutstandingInvoices();

            // Newly Added Clients
            foreach (var item in api.NewestClients())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "created",
                    DateTime = item.DateCreated,
                    Object = item.FirstName + " " + item.LastName,
                    Object_URL = "/Client/Details/" + item.ClientID,
                    User = Membership.GetUser(item.CreatorUserID.Value).UserName,
                    Icon = "icon-user-2"
                });
            }

            // Newly Updated Clients
            foreach (var item in api.UpdatedClients())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "updated",
                    DateTime = item.LastUpdated.Value,
                    Object = item.FirstName + " " + item.LastName,
                    Object_URL = "/Client/Details/" + item.ClientID,
                    User = Membership.GetUser(item.UpdateUserId.Value).UserName,
                    Icon = "icon-user-2"
                });
            }

            // Newly Updated Clients
            foreach (var item in api.DeletedClients())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "deleted",
                    DateTime = item.LastUpdated.Value,
                    Object = item.FirstName + " " + item.LastName,
                    Object_URL = "#",
                    User = Membership.GetUser(item.UpdateUserId.Value).UserName,
                    Icon = "icon-user-2"
                });
            }

            // Newly Added Events
            foreach (var item in api.NewestEvents())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "created",
                    DateTime = item.CreatedDate,
                    Object = item.Title,
                    Object_URL = "/Event/Details/" + item.EventID,
                    User = Membership.GetUser(item.CreatorUserID.Value).UserName,
                    Icon = "icon-calendar"
                });
            }

            // Updated Events
            foreach (var item in api.UpdatedEvents())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "updated",
                    DateTime = item.LastUpdated.Value,
                    Object = item.Title,
                    Object_URL = "/Event/Details/" + item.EventID,
                    User = Membership.GetUser(item.LastUpdatedUserID.Value).UserName,
                    Icon = "icon-calendar"
                });
            }

            // Newly Added Locations
            foreach (var item in api.NewestLocations())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "created",
                    DateTime = item.CreatedDate.Value,
                    Object = item.Name,
                    Object_URL = "/Location/Details/" + item.LocationID,
                    User = Membership.GetUser(item.CreatorUserID.Value).UserName,
                    Icon = "icon-location"
                });
            }

            // Updated Events
            foreach (var item in api.UpdatedLocations())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "updated", 
                    DateTime = item.LastUpdated.Value,
                    Object = item.Name,
                    Object_URL = "/Location/Details/" + item.LocationID,
                    User = Membership.GetUser(item.UpdatedUserID.Value).UserName,
                    Icon = "icon-location"
                });
            }

            // Payments
            foreach (var item in api.NewestPayments())
            {
                model.ActivityLog.Add(new Activity_VM()
                {
                    Action = "added a payment of <strong>" + item.Amount.ToString("c") + "</strong> to ",
                    DateTime = item.DateTime,
                    Object = "Invoice #" + item.InvoiceID + " (" + item.Invoice.Event.Client.FirstName + " " + item.Invoice.Event.Client.LastName  + ")",
                    Object_URL = "/Invoice/Index/" + item.InvoiceID,
                    User = Membership.GetUser(item.CreatorUserID).UserName,
                    Icon = " icon-payment"
                });
            }

            return View("Dashboard", model);
        }

        public ActionResult Error()
        {
            return View();
        }

        public ViewResult LoginInformation()
        {
            var user = Membership.GetUser(User.Identity.Name);
            return View(user);
        }
    }
}
